#include<bits/stdc++.h>
using namespace std;

int f[1005][1005];
int a[1005][1005];

void init() {
	for (int i = 1; i <= 200; i++)
		for (int j = 0; j <= i; j++)
			f[i][j] = f[i][j] = 0;
	f[0][0] = f[0][0] = 1;
}

int find_ans(int n) {
	int ans = -1;
	for (int i = 1; i <= n; i++)
		ans = max(ans, f[n][i]);
	return ans;
}

signed main() {
	int t;
	cin >> t;
	while (t--) {
		init();

		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= i; j++)
				cin >> a[i][j];

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				int t1 = f[i - 1][j - 1] * a[i][j];
				int t2 = f[i - 1][j] * a[i][j];

				t1 %= 10;
				t2 %= 10;

				f[i][j] = max(t1, t2);
			}
		}
		cout << find_ans(n) << endl;
	}
	return 0;
}
